/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset

* Sample
keep if sample_int==1 & eis_f!=. // keep observations of interest

* Globals
global match "risk tax_rate red_i mixed_dec differentiated valitrade_advice ln_iwei ln_ival_usd"

* Matching
set seed 1234
qui: psmatch2 sig_c_f $match if sample_reg==1 // matching based on risk characteristics


* ---------
* Table A12
* ---------

* Globals
global dep "ln_time fraud c1_dlog_val c1_dlog_tax htrl_f_int"
global controls "tax_rate risk red_i mixed_dec differentiated valitrade_advice"

* Matrix
forvalues x = 1/2 {
	mat tableA12_`x' = J(10,5,.)
	mat tableA12_`x'T = J(1,5,.)
}

* Panel A
* -------

* Dataset
preserve
** Neighbors
gen sample_match = . // create variable to identify matches
levelsof _n1 , local(pairs) // group according to the first nearest neighbor and store these in a local
foreach i in `pairs' {
	replace sample_match = 1 if _id==`i'
} // Loop: replace match if the psmatch id equals the nearest neighbor group value
keep if sample_match==1 | _n1!=. // keep sample of pairs

* Loop for estimations
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' eis_f $controls countryorig* max_twodigit* if sample_reg==1 , absorb(insp_f key_dec ym_*) vce(cluster insp_f key_dec)

	mat tableA12_1[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat tableA12_1[`i',`j'] = _se[eis_f]
	mat tableA12_1T[1,`j'] = e(N)
	local i = `i' + 8
	mat tableA12_1[`i',`j'] = e(r2)
	local i = `i' -9
	local j = `j' +1
}
restore


* Panel B
* -------

* Dataset
preserve
** Common support
gen weight = cond(sig_c_f==1, 1, cond(sig_c_f==0, _pscore/(1-_pscore), .)) // create weight variable that equals 1 if there is excess interaction, 0 if not, and missing otherwise
qui: sum _pscore if sig_c_f==0 [aw=weight] // capture max when there is no excess
drop if _pscore > r(max) // drop observartions without common support on the upper side
qui: sum _pscore if sig_c_f==1 [aw=weight] // capture min when there is excess
drop if _pscore < r(min) // drop observartions without common support on the lower side

* Loop for estimations
local i = 1
local j = 1
foreach x in $dep {

	qui: regwls `x' eis_f $controls countryorig* max_twodigit* insp_dum* ym_* if sample_reg==1 , absorb(key_dec) wvar(weight) robust type(abse)

	mat tableA12_2[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat tableA12_2[`i',`j'] = _se[eis_f]
	mat tableA12_2T[1,`j'] = e(N)
	local i = `i' + 8
	mat tableA12_2[`i',`j'] = e(r2)
	local i = `i' -9
	local j = `j' +1
}
restore


* Export
* ------
putexcel set "$tables\Table A12.xlsx", replace sheet(TableA12) // create a new excel spreadsheet

* Title
putexcel (A1:F1), merge hcenter vcenter
putexcel A1 = "Before delegated randomization of inspector assignment"

* Panels
** Name
putexcel (A4:F4), merge hcenter vcenter bold
putexcel A4 = "A. Matched sample"
putexcel (A17:F17), merge hcenter vcenter bold
putexcel A17 = "B. Propensity score weighted least squares estimation"
** Independent variables and coefficients
putexcel A6 = "Excess interaction share", left vcenter
putexcel B6 = matrix(tableA12_1), nformat(0.000) hcenter vcenter
putexcel A19 = "Excess interaction share", left vcenter
putexcel B19 = matrix(tableA12_2), nformat(0.000) hcenter vcenter
** Observations and coefficients
local k = 1
local r = 15
forvalues i = 14(13)27 {
	putexcel A`i' = "Observations", left vcenter
	putexcel A`r' = "R-squared", left vcenter
	putexcel B`i' = matrix(tableA12_`k'T), nformat(number_sep) hcenter vcenter
	local k = `k'+1
	local r = `r'+13
}
** Row names
local i = 8
local cells `" "B" "C" "D" "E" "F" "'
local labels `" "Declaration characteristics" "Inspector fixed effects" "Broker fixed effects" "Source country fixed effects" "HS2-product fixed effects" "Month-year fixed effects" "'
forvalues j = 1(1)6 {

	local x: word `j' of `labels'

	putexcel A`i' = "`x'", left vcenter
	local i = `i' +13
	putexcel A`i' = "`x'", left vcenter
	local i = `i'-12
}
** Column numbers
local cells `" "B" "C" "D" "E" "F" "B" "C" "D" "E" "F" "'
forvalues i = 1(1)10 {
	
	local y: word `i' of `cells'

	if `i' < 6 {
		putexcel `y'5 = "(`i')", hcenter vcenter
	}
	
	if `i' > 5 {
		putexcel `y'18 = "(`i')", hcenter vcenter
	}
}

* Column names
putexcel A2 = "Dependent variable", left vcenter
local cells `" "B" "C" "D" "E" "F" "'
local labels `" "Time" "Fraud" "\Delta log value" "\Delta log tax" "Hyp. tax revenue losses" "'
forvalues i = 1(1)5 {
	
	local x: word `i' of `labels'
	local y: word `i' of `cells'
	
	putexcel `y'2 = "`x'", hcenter vcenter bold
	
	forvalues j = 8(1)13 {
		putexcel `y'`j' = "Yes", hcenter vcenter
	}
	
	forvalues j = 21(1)26 {
		putexcel `y'`j' = "Yes", hcenter vcenter
	}
}


* -------------------------------- ENDS HERE -------------------------------- *